package com.zghl.controller;

import com.zghl.pojo.Area;
import com.zghl.pojo.System;
import com.zghl.pojo.Unit;
import com.zghl.pojo.vo.SystemVo;
import com.zghl.service.AreaService;
import com.zghl.service.SystemService;
import com.zghl.service.UnitService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/system")
public class SystemController {

    @Resource
    private SystemService systemService;
    @Resource
    private AreaService areaService;
    @Resource
    private UnitService unitService;


    @RequestMapping("/getList")
    public ModelAndView getList(){
        List<SystemVo> systemVos = systemService.systemList(null);
        ModelAndView modelAndView=new ModelAndView();

        List<Area> list = areaService.List(null);
        List<System> list1 = systemService.List(null);
        List<Unit> units = unitService.UnitList(null);

        SystemVo system=new SystemVo();
        Area area=new Area();
        Unit unit=new Unit();

        modelAndView.setViewName("systemList");
        modelAndView.addObject("sy",systemVos);
        modelAndView.addObject("arealist",list);
        modelAndView.addObject("ulist",units);
        modelAndView.addObject("s",system);
        modelAndView.addObject("area",area);
        modelAndView.addObject("unit",unit);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/list")
    public ModelAndView List(@ModelAttribute Area area){
        if (area.getId()==0&&area.getUnitId()==0){
            return  getList();
        }
        java.lang.System.out.println(area.getId()+"========="+area.getAreaName()+"========="+area.getAreaCode()+"========="+area.getUnitId());

            Map map=new HashMap();
            map.put("id",area.getId());

            List<Area> list1 = areaService.List(map);
            Area area1=new Area();
            for (Area a: list1) {
                BeanUtils.copyProperties(a,area1);
            }

            Map map1=new HashMap();
            map1.put("id",area.getUnitId());
            List<Unit> uList=unitService.UnitList(map1);
            Unit unit=new Unit();
            for (Unit u: uList) {
                BeanUtils.copyProperties(u,unit);
            }

            Map m=new HashMap();
            if (area.getId()==0){
                m.put("unitName",unit.getUnitName());
                m.put("areaName",null);
            }else if(area.getUnitId()==0){
                m.put("unitName",null);
                m.put("areaName",area1.getAreaName());
            }else {
                m.put("unitName",unit.getUnitName());
                m.put("areaName",area1.getAreaName());
            }

        java.lang.System.out.println(area1.getAreaName()+"===="+unit.getUnitName());
            List<SystemVo> systemVos = systemService.systemList(m);

            ModelAndView modelAndView=new ModelAndView();

            Map map2=new HashMap();
            map2.put("id",area.getId());

            List<Area> list = areaService.List(map2);
            List<Unit> unitList=unitService.UnitList(null);


            modelAndView.setViewName("systemList");
            modelAndView.addObject("sy",systemVos);
            modelAndView.addObject("arealist",list);
           modelAndView.addObject("ulist",unitList);
            return modelAndView;
    }

    @ResponseBody
    @GetMapping(value = "/get_area")
    public Object getArea(int unitId){
        java.lang.System.out.println(unitId);

        Map map=new HashMap();
        map.put("id",unitId);
        List<Unit> unitList =unitService.UnitList(map);

        Unit unit=new Unit();
        for (Unit u:unitList){
            BeanUtils.copyProperties(u,unit);
        }
        Map map1=new HashMap();
        map1.put("unitId",unit.getId());

        List<Area> List=areaService.List(map1);

        return List;
    }


    @RequestMapping("/add")
    public ModelAndView add(){
        List<Area> list = areaService.List(null);

        System system=new System();

        ModelAndView modelAndView=new ModelAndView();

        modelAndView.setViewName("systemAdd");
        modelAndView.addObject("s",system);
        modelAndView.addObject("l",list);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/save")
    public ModelAndView save(@ModelAttribute System system){

        Integer integer = systemService.addBySystem(system);

        if (integer>0){
            return getList();
        }
        throw  new RuntimeException("添加错误");
    }

    @RequestMapping("/delete/{Id}")
    public ModelAndView deleteSystem(@PathVariable("Id") int Id){
        Integer integer =systemService.deleteBySystem(Id);

        if (integer>0){
            return getList();
        }
        throw  new RuntimeException("删除错误");
    }

    @RequestMapping("/update/{Id}")
    public ModelAndView update(@PathVariable("Id") int Id){
        Map<String,Object> map=new HashMap<>();

        map.put("id",Id);

        List<System> list = systemService.List(map);
        List<Area> arealist = areaService.List(null);

        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("systemUpdate");

        System system=new System();

        for (System s:list) {
            BeanUtils.copyProperties(s,system);
        }
        modelAndView.addObject("s",system);
        modelAndView.addObject("a",arealist);
        modelAndView.addObject("id",Id);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/updateSvae/{Id}")
    public ModelAndView updateSystem(@PathVariable("Id") int Id,@ModelAttribute System system){
       System system1=new System();

        BeanUtils.copyProperties(system,system1);
        system1.setId(Id);

        Integer integer = systemService.updateBySystem(system1);
        if (integer>0){
            return getList();
        }
        throw  new RuntimeException("修改错误");
    }
}
